# 重跑绘图分析（代码环境已重置）

import numpy as np
import matplotlib

# 设置 matplotlib 后端为 Agg（非交互式后端）
matplotlib.use('Agg')
import matplotlib.pyplot as plt

# 设置中文字体
plt.rcParams['font.sans-serif'] = ['SimHei', 'Microsoft YaHei', 'DejaVu Sans']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

# 公共参数
contract_multiplier = 10000
short_call_strike = 1.20
short_call_price = 0.0016  # 修正：空头看涨期权价格
short_call_qty = 1000
spot_price = 1.037  # 当前标的价格
spot_position_value = 2_000_000  # 现货市值

# 方案 A：Buy 1150 Call, 137 张
call_a_strike = 1.15
call_a_price = 0.0029
call_a_qty = 137

# 方案 B：Buy 1250 Call, 1000 张
call_b_strike = 1.25
call_b_price = 0.0009
call_b_qty = 1000

# 方案 C：Buy 1150 Call 300张 + Buy 1250 Call 300张（折中结构）
call_c1_strike = 1.15
call_c1_price = 0.0029
call_c1_qty = 300
call_c2_strike = 1.25
call_c2_price = 0.0009
call_c2_qty = 300

# 方案 D：Buy 1150 Call 137张 + Buy 1250 Call 300张（混合结构）
call_d1_strike = 1.15
call_d1_price = 0.0029
call_d1_qty = 137
call_d2_strike = 1.25
call_d2_price = 0.0009
call_d2_qty = 300

# 计算不同到期价格下的盈亏（以每0.001元为一个点，从1.00到1.30）
S = np.arange(1.00, 1.31, 0.001)

# 假设标的现价 1.037 元，对应现货持仓份额：
spot_qty = int(2_000_000 / spot_price)

# 计算不同到期价格下的"整体策略"盈亏（现货 + 对冲期权组合）
total_results_a, total_results_b, total_results_c, total_results_d = [], [], [], []

for price in S:
    # === 现货 ===
    spot_pnl = (price - spot_price) * spot_qty

    # === 期权策略 A ===
    short_call_loss = np.maximum(price - short_call_strike, 0) * contract_multiplier * short_call_qty
    call_a_profit = np.maximum(price - call_a_strike, 0) * contract_multiplier * call_a_qty
    call_a_cost = call_a_price * contract_multiplier * call_a_qty
    pnl_a = spot_pnl - short_call_loss + call_a_profit - call_a_cost

    # === 期权策略 B ===
    call_b_profit = np.maximum(price - call_b_strike, 0) * contract_multiplier * call_b_qty
    call_b_cost = call_b_price * contract_multiplier * call_b_qty
    pnl_b = spot_pnl - short_call_loss + call_b_profit - call_b_cost

    # === 期权策略 C（折中结构）===
    call_c1_profit = np.maximum(price - call_c1_strike, 0) * contract_multiplier * call_c1_qty
    call_c1_cost = call_c1_price * contract_multiplier * call_c1_qty
    call_c2_profit = np.maximum(price - call_c2_strike, 0) * contract_multiplier * call_c2_qty
    call_c2_cost = call_c2_price * contract_multiplier * call_c2_qty
    pnl_c = spot_pnl - short_call_loss + call_c1_profit - call_c1_cost + call_c2_profit - call_c2_cost

    # === 期权策略 D（混合结构）===
    call_d1_profit = np.maximum(price - call_d1_strike, 0) * contract_multiplier * call_d1_qty
    call_d1_cost = call_d1_price * contract_multiplier * call_d1_qty
    call_d2_profit = np.maximum(price - call_d2_strike, 0) * contract_multiplier * call_d2_qty
    call_d2_cost = call_d2_price * contract_multiplier * call_d2_qty
    pnl_d = spot_pnl - short_call_loss + call_d1_profit - call_d1_cost + call_d2_profit - call_d2_cost

    total_results_a.append(pnl_a)
    total_results_b.append(pnl_b)
    total_results_c.append(pnl_c)
    total_results_d.append(pnl_d)

# 转换为 numpy 数组
total_results_a = np.array(total_results_a)
total_results_b = np.array(total_results_b)
total_results_c = np.array(total_results_c)
total_results_d = np.array(total_results_d)

# 绘图
plt.figure(figsize=(14, 9))
plt.plot(S, total_results_a, label='方案 A：Buy 1150 Call × 137', color='blue', linewidth=2)
plt.plot(S, total_results_b, label='方案 B：Buy 1250 Call × 1000', color='green', linewidth=2)
plt.plot(S, total_results_c, label='方案 C：Buy 1150 Call × 300 + Buy 1250 Call × 300', color='red', linewidth=2)
plt.plot(S, total_results_d, label='方案 D：Buy 1150 Call × 137 + Buy 1250 Call × 300', color='purple', linewidth=2)

# 添加关键价格线
plt.axvline(short_call_strike, color='gray', linestyle='--', label='Sell Call Strike (1.20)', alpha=0.7)
plt.axvline(call_a_strike, color='lightblue', linestyle=':', label='1150 Call Strike', alpha=0.7)
plt.axvline(call_b_strike, color='lightgreen', linestyle=':', label='1250 Call Strike', alpha=0.7)
plt.axvline(spot_price, color='orange', linestyle='-.', label='现货买入价 (1.037)', alpha=0.7)
plt.axhline(0, color='black', linestyle='--', alpha=0.5)

plt.xlabel('标的价格（到期）', fontsize=12)
plt.ylabel('总盈亏（元）', fontsize=12)
plt.title('四个风险对冲方案的盈亏对比（含现货持仓）', fontsize=14, fontweight='bold')
plt.legend(fontsize=10)
plt.grid(True, alpha=0.3)
plt.tight_layout()

# 保存图片
plt.savefig('option_plan_analysis.png', dpi=300, bbox_inches='tight')
print("图片已保存为 option_plan_analysis.png")

# 打印关键数据点分析
print("\n=== 方案对比分析 ===")
print(f"现货持仓：{spot_qty:,} 份，市值 {spot_position_value:,} 元")
print(f"空头看涨期权：{short_call_qty} 张，行权价 {short_call_strike}，价格 {short_call_price}")

print(f"\n各方案成本：")
print(f"方案A成本：{call_a_price * contract_multiplier * call_a_qty:,.0f} 元")
print(f"方案B成本：{call_b_price * contract_multiplier * call_b_qty:,.0f} 元")
print(
    f"方案C成本：{(call_c1_price * contract_multiplier * call_c1_qty + call_c2_price * contract_multiplier * call_c2_qty):,.0f} 元")
print(
    f"方案D成本：{(call_d1_price * contract_multiplier * call_d1_qty + call_d2_price * contract_multiplier * call_d2_qty):,.0f} 元")

# 分析在关键价格点的表现
key_prices = [1.15, 1.20, 1.25, 1.30]
print(f"\n关键价格点表现：")
for price in key_prices:
    idx = int((price - 1.00) / 0.001)
    if idx < len(total_results_a):
        print(
            f"价格 {price:.2f}: A={total_results_a[idx]:,.0f}, B={total_results_b[idx]:,.0f}, C={total_results_c[idx]:,.0f}, D={total_results_d[idx]:,.0f}")

# 计算最大回撤和最大盈利
print(f"\n=== 风险收益分析 ===")
for name, results in [("A", total_results_a), ("B", total_results_b), ("C", total_results_c), ("D", total_results_d)]:
    max_profit = np.max(results)
    max_loss = np.min(results)
    print(f"方案{name}: 最大盈利 {max_profit:,.0f} 元, 最大亏损 {max_loss:,.0f} 元")
